﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CarsModel;
using CarsDal;

namespace CarsBll
{
    
    public class IndentsBll
    {
        IndentsDal IndentDal = new IndentsDal();
        UsersDal UsersDal = new UsersDal();
        IndentsRecordsDal IndentsRecDal = new IndentsRecordsDal();
        GoodsDal GoodsDal = new GoodsDal();
        /// <summary>
        /// 订单添加功能
        /// </summary>
        /// <param name="indents"></param>
        /// <returns></returns>
        public int IndentADD(Indents indents)
        {
            indents.IndentState = 0;
            indents.IndentTime = DateTime.Now;
            int i = IndentDal.IndentAdd(indents);
            //修改上架商品的状态
            Goods goods = GoodsDal.GoodsList().Where(p => p.GoodsID == indents.GoodsID).FirstOrDefault();
            goods.GoodsState = 2;
            GoodsDal.GoodsUpdate(goods);
            return i;
        }
        /// <summary>
        /// 购买成功后删除订单
        /// </summary>
        /// <param name="Indentid">要删除的订单ID</param>
        /// <param name="UsersID">当前登录用户的ID</param>
        /// <returns></returns>
        public int IndentDelete(int Indentid,int UsersID)
        {
            List<Indents> IndentsList = IndentDal.IndentShow(UsersID);
            Indents indents = IndentsList.Where(p => p.IndentId == Indentid).FirstOrDefault();

            Goods Goods=GoodsDal.GoodsList().Where(p=>p.GoodsID==indents.GoodsID).FirstOrDefault();

            Users Users=UsersDal.LoginList().Where(p=>p.UsersID==Goods.UsersID).FirstOrDefault();

            IndentsRecords IndentsRecords = new IndentsRecords();
            IndentsRecords.ConsigeeTel = indents.ConsigeeTel;
            IndentsRecords.ConsigneeAddress = indents.ConsigneeAddress;
            IndentsRecords.ConsigneeName = indents.ConsigneeName;
            IndentsRecords.GoodsImage = Goods.GoodsImage;
            IndentsRecords.GoodsPrice = Goods.GoodsPrice;
            IndentsRecords.IndentPrice = indents.IndentPrice;
            IndentsRecords.IndentTime = indents.IndentTime;
            IndentsRecords.UsersID = indents.UsersID;
            IndentsRecords.UsersName = Users.UsersName;
            IndentsRecords.UsersTel = Users.UsersTel;
            IndentsRecDal.IndentsRecordsAdd(IndentsRecords);

            int i = IndentDal.IndentUpdate(2, Indentid);

            return i;

        }
        
    }
}
